package cn.itsource.user.service.impl;

import cn.itsource.basic.constants.BaseConstant;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.basic.vo.PhoneRegisterVo;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.LogininfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mochen
 * @since 2023-01-08
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private UserMapper userMapper;
    /*用户注册接口实现*/
    /*1.参数非空校验
    * 2.确认密码和密码是否一致
    * 3.检查手机验证码是否正确
    * 4.先保存t_logininfo,再存t_user*/
    @Override
    public void phoneRegister(PhoneRegisterVo phoneRegisterVo) {
//        1.参数非空校验

//        2.确认密码和密码是否一致
        if (!phoneRegisterVo.getPassword().equals(phoneRegisterVo.getPasswordRepeat())){
            throw new BusinessException("两次输入密码不一致");
        }
//        3.检查手机验证码是否正确
        Object smsCodeAndTime = redisTemplate.opsForValue().get(BaseConstant.VerifyConstants.USER_PHONE_REGISTER_CODE_PREFIX + phoneRegisterVo.getPhone());//4561:56464651032
        if (smsCodeAndTime == null){
            throw new BusinessException("手机验证码已过期");
        }
        String[] smsCodeAndTimeArr = smsCodeAndTime.toString().split(":");
        if (!smsCodeAndTimeArr[0].equals(phoneRegisterVo.getSmsCode())){
            throw new BusinessException("手机验证码输入有误");
        }
//        4.先保存t_logininfo,再存t_user
        User user = phoneRegisterVo2User(phoneRegisterVo);
        Logininfo logininfo = user2Logininfo(user);

        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);

    }


    //初始化logininfo
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);

        logininfo.setDisable(false);
        logininfo.setType(true);

        return logininfo;
    }

    //初始化user
    private User phoneRegisterVo2User(PhoneRegisterVo phoneRegisterVo) {
        User user = new User();
        String salt = StrUtils.getComplexRandomString(32);

        user.setSalt(salt);
        user.setPhone(phoneRegisterVo.getPhone());
        user.setState(1);
        //加密,加盐
        user.setPassword(MD5Utils.encrypByMd5(phoneRegisterVo.getPassword()) + salt);
        user.setUsername(phoneRegisterVo.getPhone());
        user.setCreatetime(new Date());

        return user;
    }
}
